1
|
8 |
|
import { Controller, Get, Param, Post, UseGuards, Req } from '@nestjs/common'; |
2
|
8 |
|
import { ApiOperation, ApiResponse, ApiBearerAuth, ApiParam, ApiTags } from '@nestjs/swagger'; |
3
|
8 |
|
import { JwtAuthGuard } from '../auth/guards/jwt-auth.guard'; |
4
|
8 |
|
import { TravelService } from './travel.service'; |
5
|
8 |
|
import { TravelResponseDto } from './dto/renting.dto'; |
6
|
|
|
|
7
|
|
|
@ApiTags('Bike Rentals') |
8
|
|
|
@Controller({ path: 'rental', version: '1' }) |
9
|
8 |
|
export class TravelController { |
10
|
12 |
|
constructor(private readonly travelService: TravelService) {} |
11
|
|
|
|
12
|
|
|
@Get('customer/:customerId') |
13
|
|
|
// @UseGuards(JwtAuthGuard) |
14
|
|
|
// @ApiBearerAuth() |
15
|
|
|
@ApiOperation({ |
16
|
|
|
summary: 'Get all travels for a customer', |
17
|
|
|
description: 'Returns all travels for a specific customer', |
18
|
|
|
}) |
19
|
|
|
@ApiResponse({ |
20
|
|
|
status: 200, |
21
|
|
|
description: 'Travels found', |
22
|
|
|
type: [TravelResponseDto], |
23
|
|
|
}) |
24
|
|
|
@ApiResponse({ |
25
|
|
|
status: 404, |
26
|
|
|
description: 'No travels found for this customer', |
27
|
|
|
}) |
28
|
8 |
|
async getTravelsForCustomer(@Param('customerId') customerId: string) { |
29
|
2 |
|
return await this.travelService.findTravelsForCustomer(customerId); |
30
|
|
|
} |
31
|
|
|
|
32
|
|
|
@Get('bike/:bikeId/active') |
33
|
|
|
@ApiOperation({ |
34
|
|
|
summary: 'Get active travel for a bike', |
35
|
|
|
description: |
36
|
|
|
'Returns the active travel information including renter details for a specific bike', |
37
|
|
|
}) |
38
|
|
|
@ApiResponse({ |
39
|
|
|
status: 200, |
40
|
|
|
description: 'Active travel found', |
41
|
|
|
type: TravelResponseDto, |
42
|
|
|
}) |
43
|
|
|
@ApiResponse({ |
44
|
|
|
status: 404, |
45
|
|
|
description: 'No active travel found for this bike', |
46
|
|
|
}) |
47
|
8 |
|
async getActiveTravelForBike(@Param('bikeId') bikeId: string) { |
48
|
3 |
|
return await this.travelService.findActiveTravelForBike(bikeId); |
49
|
|
|
} |
50
|
|
|
|
51
|
|
|
@Post('bike/:bikeId/end-active') |
52
|
|
|
@ApiOperation({ |
53
|
|
|
summary: 'End active travel for specific bike', |
54
|
|
|
description: 'Ends the currently active travel for the specified bike', |
55
|
|
|
}) |
56
|
|
|
@ApiResponse({ |
57
|
|
|
status: 201, |
58
|
|
|
description: 'Travel ended successfully', |
59
|
|
|
}) |
60
|
|
|
@ApiResponse({ |
61
|
|
|
status: 404, |
62
|
|
|
description: 'No active travel found for this bike', |
63
|
|
|
}) |
64
|
8 |
|
async endActiveBikeTravel(@Param('bikeId') bikeId: string) { |
65
|
2 |
|
return await this.travelService.endActiveTravelForBike(bikeId); |
66
|
|
|
} |
67
|
|
|
|
68
|
|
|
@Post(':githubId/end-all-travels') |
69
|
|
|
@UseGuards(JwtAuthGuard) |
70
|
|
|
@ApiBearerAuth() |
71
|
|
|
@ApiOperation({ |
72
|
|
|
summary: 'End all active travels for a customer', |
73
|
|
|
description: 'Ends all active travels for the specified customer by GitHub ID.', |
74
|
|
|
}) |
75
|
|
|
@ApiParam({ |
76
|
|
|
name: 'githubId', |
77
|
|
|
description: 'The GitHub ID of the user whose travels are to be ended.', |
78
|
|
|
example: '12345', |
79
|
|
|
}) |
80
|
|
|
@ApiResponse({ |
81
|
|
|
status: 200, |
82
|
|
|
description: 'All active travels for the customer have been ended.', |
83
|
|
|
}) |
84
|
|
|
@ApiResponse({ |
85
|
|
|
status: 404, |
86
|
|
|
description: 'Customer or active travels not found.', |
87
|
|
|
}) |
88
|
8 |
|
async endAllTravelsForCustomer(@Param('githubId') githubId: string) { |
89
|
2 |
|
return await this.travelService.endAllTravelsForCustomer(githubId); |
90
|
|
|
} |
91
|
|
|
|
92
|
|
|
// Start a bike rental |
93
|
|
|
@Post('bike/:id') |
94
|
|
|
@UseGuards(JwtAuthGuard) |
95
|
|
|
@ApiBearerAuth() |
96
|
|
|
@ApiOperation({ |
97
|
|
|
summary: 'Start renting a bike', |
98
|
|
|
description: |
99
|
|
|
'Initiates a bike travel for the authenticated user. The bike must be available for travel.', |
100
|
|
|
}) |
101
|
|
|
@ApiResponse({ |
102
|
|
|
status: 201, |
103
|
|
|
description: 'Bike travel started successfully', |
104
|
|
|
type: TravelResponseDto, |
105
|
|
|
}) |
106
|
|
|
@ApiResponse({ |
107
|
|
|
status: 400, |
108
|
|
|
description: 'Bad Request - Bike not found or not available for renting', |
109
|
|
|
}) |
110
|
|
|
@ApiResponse({ |
111
|
|
|
status: 401, |
112
|
|
|
description: 'Unauthorized - User not authenticated', |
113
|
|
|
}) |
114
|
8 |
|
async startRentingBike(@Param('id') id: string, @Req() req: any) { |
115
|
4 |
|
const userId = req.user.githubId; |
116
|
4 |
|
return this.travelService.startRentingBike(id, userId); |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
// End a bike travel |
120
|
|
|
@Post(':id/end') |
121
|
|
|
// removing the guard for now, the bike software should be able to end a travel. |
122
|
|
|
// we could implement a guard that authenticates the bike somehow |
123
|
|
|
// we could also expand on this, making it even more complex, like checking that it is the user whom initlized the rental that is ending it |
124
|
|
|
// @UseGuards(JwtAuthGuard) |
125
|
|
|
@ApiOperation({ |
126
|
|
|
summary: 'End a bike travel', |
127
|
|
|
description: 'Ends the travel, calculates cost, and makes the bike available again', |
128
|
|
|
}) |
129
|
|
|
@ApiResponse({ |
130
|
|
|
status: 201, |
131
|
|
|
description: 'Travel ended successfully', |
132
|
|
|
}) |
133
|
|
|
@ApiResponse({ |
134
|
|
|
status: 400, |
135
|
|
|
description: 'Bad Request - Travel not found or already ended', |
136
|
|
|
}) |
137
|
|
|
@ApiResponse({ |
138
|
|
|
status: 401, |
139
|
|
|
description: 'Unauthorized', |
140
|
|
|
}) |
141
|
8 |
|
async endTravel(@Param('id') travelId: number) { |
142
|
1 |
|
return this.travelService.endTravel(travelId); |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
// Fetch one travel by ID |
146
|
|
|
@Get(':id') |
147
|
|
|
@ApiResponse({ |
148
|
|
|
status: 200, |
149
|
|
|
description: 'Travel found', |
150
|
|
|
type: TravelResponseDto, |
151
|
|
|
}) |
152
|
8 |
|
async getTravelById(@Param('id') id: number) { |
153
|
2 |
|
return await this.travelService.findById(id); |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
// Fetch all travels |
157
|
|
|
@Get() |
158
|
8 |
|
async getAllTravels() { |
159
|
2 |
|
return await this.travelService.findAll(); |
160
|
|
|
} |
161
|
|
|
} |
162
|
|
|
|